package as.service.impl;

import java.util.List;

import as.dao.RecyclerDAO;
import as.dao.RegisterDAO;
import as.entity.Register;
import as.entity.Student;
import as.service.RegisterService;
    
public class RegisterServiceImpl implements RegisterService {

	private RegisterDAO registerDAO;
	private RecyclerDAO recyclerDAO;
   
	public RecyclerDAO getRecyclerDAO() {
		return recyclerDAO;
	}

	public void setRecyclerDAO(RecyclerDAO recyclerDAO) {
		this.recyclerDAO = recyclerDAO;
	}
 
	public RegisterDAO getRegisterDAO() { 
		return registerDAO;
	}

	public void setRegisterDAO(RegisterDAO registerDAO) {
		this.registerDAO = registerDAO;
	}

	public void saveRegister(Register register) {

		if (this.recyclerDAO.isRecyclerHas()) {
			register.setRegisterNo(this.recyclerDAO.findRecyclerFirst());
			this.registerDAO.saveRegister(register);
			this.recyclerDAO.removeRecycler(this.recyclerDAO
					.findRecycler(this.recyclerDAO.findRecyclerFirst()));

		} else {
			if(this.registerDAO.IsRegisterNull()){
				register.setRegisterNo(00001);
				this.registerDAO.saveRegister(register);
			}else{
				int i = this.registerDAO.findRegisterMax().getRegisterNo();
				register.setRegisterNo(i + 1);
				this.registerDAO.saveRegister(register);
			}

		}
	}

	public Register findRegisterMax() {

		return this.registerDAO.findRegisterMax();
	}

	public boolean IsRegisterNull() {
		return this.registerDAO.IsRegisterNull();
	}

	public void RemoveRegisterList(List<Register> list) {
		this.registerDAO.RemoveRegisterList(list);

	}

	public List<Register> findAllRegistersByCourseId(long id) {

		return this.registerDAO.findAllRegistersByCourseId(id);

	}

	public Register findRegisteris(Integer id) {
		// TODO Auto-generated method stub
		return this.registerDAO.findRegisteris(id);
	}

	public List<Student> findAllStudentByCourseId(long id) {
	return this.registerDAO.findAllStudentByCourseId(id);
	}

	public List<Register> findRegisteByStudentId(long id) {
		// TODO Auto-generated method stub
		return this.registerDAO.findRegisteByStudentId(id);
	}

	public boolean IsStudentRepeat(long id) {
	
          if(this.registerDAO.findRegisteByStudentId(id).size()>=2){
        	  return true;
          }else{
        	  return false;
          }
		
	}

	public List<Student> findAllStudentByStudentId(long id) {
		return this.registerDAO.findAllStudentByStudentId(id);
	}

	public void removeRegister(Register register) {
		this.registerDAO.removeRegister(register);
	}

	public int numberOfStudent(long id) {
		List<Register> list =this.registerDAO.findRegisteByStudentId(id);
	
		return list.size();
		
	}
	

}
